package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.NursingProjectPageQueryDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    NursingProjectMapper nursingProjectMapper;
    @Autowired
    NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    //@Cacheable(cacheNames = "zzyl:nurpro",key = "#root.methodName+#dto.hashCode()")
    public PageBean<NursingProjectVo> pagequery(NursingProjectPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingProjectVo> query1 = nursingProjectMapper.query(dto);
        return PageBean.of(query1, NursingProjectVo.class);
    }

    @Override
    @CacheEvict(cacheNames = "zzyl:nurpro",allEntries = true)
    public void add(NursingProjectDto dto) {
        NursingProject nursingProject1 = BeanUtil.toBean(dto, NursingProject.class);
        nursingProject1.setCreateTime(LocalDateTime.now());
        nursingProject1.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.add(nursingProject1);
    }

    @Override
    @Cacheable(cacheNames = "zzyl:nurpro",key = "#root.methodName+#id")
    public NursingProjectVo selectid(Long id) {
        NursingProjectVo vo = nursingProjectMapper.selectid(id);
        return vo;
    }

    @Override
    @CacheEvict(cacheNames = "zzyl:nurpro",allEntries = true)
    public void update(NursingProjectDto dto) {
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    @CacheEvict(cacheNames = "zzyl:nurpro",allEntries = true)
    public void status(Long id, Integer status) {
        NursingProject build = NursingProject.builder().id(id).status(status).build();
        nursingProjectMapper.update(build);
    }

    @Override
    @CacheEvict(cacheNames = "zzyl:nurpro",allEntries = true)
    public void deleteid(Long id) {
        NursingProjectVo selectid = nursingProjectMapper.selectid(id);
        if (selectid.getStatus()==1){
            throw new BaseException(BasicEnum.DELETE_PROJECT_STATUS_FAIL);
        }

        List<NursingProjectPlan> list= nursingProjectPlanMapper.selectByProId(id);
        if (CollUtil.isEmpty(list)){
            throw new BaseException(BasicEnum.DELETE_PROJECT_FAIL);
        }
        nursingProjectMapper.deleteid(id);
    }

    @Override
    @Cacheable(cacheNames = "zzyl:nurpro",key = "#root.methodName")
    public List<NursingProject> selectall() {
        List<NursingProject> nursingProjects = nursingProjectMapper.selectall();
        return nursingProjects;
    }

    @Override
    @Cacheable(cacheNames = "zzyl:nurpro",key = "#root.methodName+#dto.hashCode()")
    public PageBean<NursingProjectVo> pagequery2(NursingProjectPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Page<NursingProjectVo> query2 = nursingProjectMapper.query2(dto);
        return PageBean.of(query2, NursingProjectVo.class);
    }

    @Override
    @CacheEvict(cacheNames = "zzyl:nurpro",allEntries = true)
    public void add2(NursingProjectDto dto) {
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProjectMapper.add2(nursingProject);
    }
}
